package com.guidance.guidance.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.guidance.guidance.domain.Message;
import com.guidance.guidance.domain.User;
import com.guidance.guidance.repository.UserRepository;

@RestController
@RequestMapping("/login")
public class LoginController {

//	private boolean phoneHave = false;
//	private boolean passHave = false;
//	private boolean flgHave = false;

	@Autowired
	private UserRepository userRepository;

	
	/**
	 * 登录Controller，客户端可通过此API实现登录,所需传入参数有telephone,password,flg
	 * @param user
	 * @param msg
	 * @return
	 */
	@RequestMapping("/userLogin")
	public Message<User> userLogin(User user, Message<User> msg) {
		User userOne = userRepository.findByTelephone(user.getTelephone());
		if (userOne == null) {
			msg.setCode("404");
			msg.setMsg("没有该用户");
			msg.setSuccess("false");
		}else if (!userOne.getPassword().equals(user.getPassword())) {
			msg.setCode("404");
			msg.setMsg("密码错误");
			msg.setSuccess("false");
		} else if (userOne.getFlg() != user.getFlg()) {
			msg.setCode("404");
			msg.setMsg("该用户类型不正确");
			msg.setSuccess("false");
		} else{
			msg.setCode("200");
			msg.setMsg("登录成功");
			msg.setSuccess("true");
			msg.setData(userOne);
			return msg;
		}
		return msg;
	}

}
